package com.springboot.cloud.sysadmin.organization.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.cloud.common.core.utils.StringUtils;
import com.springboot.cloud.sysadmin.organization.dao.LogininfoMapper;
import com.springboot.cloud.sysadmin.organization.entity.param.LogininfoQueryParam;
import com.springboot.cloud.sysadmin.organization.entity.po.Logininfo;
import com.springboot.cloud.sysadmin.organization.service.ILogininfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Objects;
import java.util.Set;

/**
 * Created by ZengJian on 2021/2/3 0003 上午 9:22
 */
@Service
@Slf4j
public class LogininfoService extends ServiceImpl<LogininfoMapper, Logininfo> implements ILogininfoService {

    /**
     * 新增操作日志
     *
     * @param logininfo 操作日志对象
     */
    @Override
    public boolean add(Logininfo logininfo) {
        return this.saveOrUpdate(logininfo);
    }

    /**
     * 批量删除系统操作日志
     *
     * @param ids 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public boolean delete(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            log.debug("ids:{}", ids);
            return false;
        }
        return this.removeByIds(ids);
    }

    /**
     * 查询操作日志详细
     *
     * @param id 操作ID
     * @return 操作日志对象
     */
    @Override
    public Logininfo get(String id) {
        return this.getById(id);
    }

    @Override
    public boolean getLogininfo(String id) {
        Logininfo logininfo = this.getById(id);
        if (StringUtils.isNull(logininfo)) {
            return false;
        }
        return true;
    }

    @Override
    public IPage<Logininfo> query(Page page, LogininfoQueryParam logininfoQueryParam) {
        SimpleDateFormat convert = new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<Logininfo> queryWrapper = logininfoQueryParam.build();
        queryWrapper.like(StringUtils.isNotBlank(logininfoQueryParam.getAccount()), "account", logininfoQueryParam.getAccount());
        queryWrapper.like(StringUtils.isNotBlank(logininfoQueryParam.getIpaddr()), "ipaddr", logininfoQueryParam.getIpaddr());
        queryWrapper.like(StringUtils.isNotBlank(logininfoQueryParam.getLoginLocation()), "loginLocation", logininfoQueryParam.getLoginLocation());
        queryWrapper.like(StringUtils.isNotBlank(logininfoQueryParam.getBrowser()), "browser", logininfoQueryParam.getBrowser());
        queryWrapper.like(StringUtils.isNotBlank(logininfoQueryParam.getOs()), "os", logininfoQueryParam.getOs());
        queryWrapper.like(StringUtils.isNotBlank(logininfoQueryParam.getMsg()), "msg", logininfoQueryParam.getMsg());
        if (logininfoQueryParam.getCreatedTimeStart() != null && logininfoQueryParam.getCreatedTimeEnd() != null) {
            queryWrapper.apply("date_format(created_time,'%Y-%m-%d') >= date_format('" + convert.format(logininfoQueryParam.getCreatedTimeStart()) + "','%Y-%m-%d')");
            queryWrapper.apply("date_format(created_time,'%Y-%m-%d') <= date_format('" + convert.format(logininfoQueryParam.getCreatedTimeEnd()) + "','%Y-%m-%d')");
        }
        return this.page(page, queryWrapper);
    }

    @Override
    public boolean update(Logininfo logininfo) {
        Logininfo logininfo1 = this.getById(logininfo.getId());
        if (StringUtils.isNull(logininfo1)) {
            return false;
        }
        return this.saveOrUpdate(logininfo);
    }
}
